<template>
  <div class="count">
    <a class="btn btn-decrease" href="javascript:;" v-if="currentValue > 0" @click="decrease">-</a>
    <span class="number" v-if="currentValue > 0">{{currentValue}}</span>
    <a class="btn btn-increase" href="javascript:;" @click="increase">+</a>
  </div>
</template>

<script>
export default {
  name: 'Count',
  props: {
    value: {
      type: Number,
      default: 0
    }
  },

  data() {
    return {
      currentValue: this.value
    }
  },

  watch: {
    value(val) {
      this.currentValue = val
    }
  },

  methods: {
    decrease(evt) {
      // if (!evt._constructed) {
      //   return
      // }

      this.currentValue--
      if (this.currentValue < 0) {
        this.currentValue = 0
      }
      this.$emit('change', this.currentValue)
      this.$emit('input', this.currentValue)
    },

    increase(evt) {
      // if (!evt._constructed) {
      //   return
      // }

      this.currentValue++
      this.$emit('change', this.currentValue)
      this.$emit('input', this.currentValue)
    }
  }
}
</script>

<style lang="less" scoped>
.count {
  .btn, .number {
    display: inline-block;
    font-size: 14px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    line-height: 32px;
    text-align: center;
  }

  .btn {
    background-color: #41B883;
    color: #fff;
  }
}
</style>
